$(document).ready(function(){
	var ThisForm = $('form#generatebalancereport');
	var PdfButton = $('button#pdf');
	var ExcelButton = $('button#excel');
	var url = ThisForm.attr('action');

	//create a new div
	var newDiv = document.createElement("div");
	var newDivChild = document.createElement("p"); 
	newDiv.id = "DOMdialog";
	newDivChild.className = "content";
	newDiv.appendChild(newDivChild)
	document.body.insertBefore(newDiv, document.body.childNodes[0]);
	
	//initiate jquery UI dialog
	var dialogDiv = $('div#DOMdialog');

	$(dialogDiv).dialog({
		autoOpen: false,
		show: "blind",
		hide: "explode",
		resizable : false,
		draggable: false,
		minHeight: 400,
		minWidth: 200,
		modal:true,
		open: function( event, ui ) {
			$(document).find('body').css('overflow','hidden');
		},
		close:function(){
			$(document).find('body').css('overflow','scroll');	
		},
		title:'Confirm Action.'
	});


	ThisForm.on('submit',function(e){
		e.preventDefault();
	});

	PdfButton.on('click',function(){
		var formdata = ThisForm.serializeArray();
		formdata.push({
		    name: "type",
		    value: "pdf"
		});

		process(formdata);
	});

	ExcelButton.on('click',function(){
		var formdata = ThisForm.serializeArray();
		formdata.push({
		    name: "type",
		    value: "excel"
		});
		
		process(formdata);
	});


	function process(serializedData)
	{
		var param = $.param(serializedData)
		dialogDiv.find('p.content').text( 'Continue and generate report?' );
		dialogDiv.dialog('option', 'buttons', {
            "Confirm" : function() {
            	window.open(url+"?"+param);
				$(this).dialog("close");
            },
            "Cancel" : function() { $(this).dialog("close"); }
   	 	});

		dialogDiv.dialog("open");
	}

});